#!/bin/sh

#source var/function
. ./function.sh
inst_launch_workaround

PATH=./:$PATH
print_start() {
  #    more << EOF
  echo "Athena Starting..."
  echo "            (c) 2013 rAthena Project"
  echo ""
  echo ""
  echo "checking..."
  #EOF
}

get_status(){
   PIDFILE=.$1.pid
   if [ -e ${PIDFILE} ]; then
    ISRUN=$(ps ax | grep $(cat ${PIDFILE}) | grep $1)
    PSRUN=$(echo "$ISRUN" | awk '{ print substr( $0, 0, 7) }')
   fi
   return $PSRUN
}

#cheking if already started, launch and mark in log
start_serv(){ 
  LOGFILE="log/$1.log"
  get_status $1
  echo "My logfile=${LOGFILE}"
  if [ -z ${PSRUN} ]; then
    exec ./$1&
    echo $! > .$1.pid
    echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`"
    echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`" >> ${LOGFILE}
  else
    echo "Can't start '$1', cause is already running p${PSRUN}"
    echo "Can't start '$1', cause is already running p${PSRUN}" >> ${LOGFILE}
  fi
}

#experimental
watch_serv(){
  LOGFILE=./log/$1.log
  ulimit -Sc unlimited

  while true; do
   get_status $1
   if [ -z ${PSRUN} ]; then
     $count=$count+1
     echo "server '$1' is down" >> ${LOGFILE}
     echo "restarting server at time at `date +"%m-%d-%H:%M-%S"`">> ${LOGFILE}
     exec ./$1&
     echo $! > .$1.pid   
     if [ $3 ] && [ $3 -lt $count ]; then break; fi
   else sleep $2    
   fi
  done
}

restart(){
  $0 stop
  for i in ${L_SRV} ${C_SRV} ${M_SRV}
    do
    while true; do
	get_status ${i}
	if [ ${PSRUN} ]; then echo "'${i}' is still running p${PSRUN} waiting end"; sleep 2;
        else break; fi
    done
  done
  $0 start
}

case $1 in
    'start')
        print_start
        check_files
	echo "Check complete."
	echo "Looks good, a nice Athena!"
	for i in ${L_SRV} ${C_SRV} ${M_SRV}
	    do
		start_serv $i
	    done
        echo "Now Started Athena."
;;
    'watch')
	for i in ${L_SRV} ${C_SRV} ${M_SRV}
	    do		
		watch_serv $i 10 3
	    done
 	echo "Now watching Athena."
;;	
    'stop')
	for i in ${L_SRV} ${C_SRV} ${M_SRV}
	    do
		PIDFILE=.${i}.pid
		if [ -e ./${PIDFILE} ]; then
		    kill $(cat ${PIDFILE})
		    rm ${PIDFILE}
		fi
	    done
;;
    'restart')
         restart
;;
    'status')
	for i in ${L_SRV} ${C_SRV} ${M_SRV}
	do
		get_status ${i}
		if [ ${PSRUN} ]; then echo "'${i}' is running p${PSRUN}"; else echo "'${i}' seem down"; fi
	done
;;	
    *)
        echo "Usage: athena-start { start | stop | restart | status | watch }"
        read -p "Enter a valid option: " readEnterKey
		$0 $readEnterKey
;;
esac


